package net.oschina.app.testcases.favorites;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.robotium.solo.Solo;

import net.oschina.app.AppStart;

/**
 * Created by Administrator on 2017/7/1.
 */

public class BasicTestCase extends ActivityInstrumentationTestCase2 {

    private SoloWrap solo;
    private Solo solo1;
    protected Helper helper;
    private boolean isNeedLogin;

    public BasicTestCase() {
        super(AppStart.class);
    }

    public void setUp (boolean isNeedLogin) throws Exception {
        this.isNeedLogin = isNeedLogin;
        this.setUp();
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        solo1 = new Solo(getInstrumentation(), getActivity());
        solo = new SoloWrap(solo1);
        helper = new Helper(solo);
        //需要登录的情况
        if(isNeedLogin){
            helper.getPageNavigation().clickMyTab();
            if (!helper.getPageCommon().waitForText("autotest",2)){
                helper.getPageMyTab().clickLoginIcon();
                helper.getPageLogin().login("735723619@qq.com","12345678");
            }
        }else {
            //不需要登录的情况
            helper.getPageNavigation().clickMyTab();
            if (helper.getPageCommon().waitForText("autotest",2)){
                helper.getPageMyTab().clickSettingsImageBtn();
                helper.getPageSettings().clickLogout();
            }
        }
        helper.getPageNavigation().clickNewsTab();
    }

    @Override
    public void tearDown() throws Exception {
        try {

        }catch (Exception ex){
            //异常控制  截图
            Log.i("AUTO","Excpetion happened in tearDown sceiont,the exception msg is "+ex.getMessage());
            solo.takeScreenShot(this.getClass().getSimpleName());
            throw ex;
        }finally {
            solo1.finishOpenedActivities();
            super.tearDown();
        }
    }

    @Override
    protected void runTest() throws Throwable {
        try {
            super.runTest();
        }catch (Throwable th){
            Log.i("AUTO","Excpetion happened in runtest sceiont,the exception msg is "+th.getMessage());
            solo.takeScreenShot(this.getClass().getSimpleName());
            throw th;
        }


    }
}
